package com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.agency.chlid

import android.os.Bundle
import android.util.Log
import android.view.View
import com.example.library_common.album.view.grid_view.XGridLayout
import com.example.library_common.album.view.grid_view.adapter.CommAdapter
import com.example.library_common.album.view.grid_view.adapter.XQuickViewHolder
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.utils.normalShortToast
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.agency.AgencySubPageInfo
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.agency.bean.AgencyCenterAreaBean
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.agency.bean.AgencyProxyIndexBean
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.agency.config.CITY
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.agency.config.DISTRICT
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.agency.config.PROVINCE
import com.example.shushiyunsst.fragment.main.home.child.new_sale.recycler.bean.NewSaleFunEntity
import com.example.shushiyunsst.util.HiDataBus

/**
 * 代理中心
 * */
class AgencyCenterFragment : BaseFragmentEx(), AgencySubPageInfo {

    override val title = R.string.agency_center

    override val icon = R.mipmap.agency_center

    companion object {
        fun newInstance(): AgencyCenterFragment {
            val args = Bundle()

            val fragment = AgencyCenterFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var mXGridLayout: XGridLayout? = null

    private var mAdapter: CommAdapter<AgencyCenterAreaBean>? = null

    private val ls = mutableListOf<AgencyCenterAreaBean>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport = false
    }

    override fun getLayoutId(): Any {
        return R.layout.fragment_agency_center
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        mXGridLayout = view.findViewById(R.id.mXGridLayout)

        mAdapter = object : CommAdapter<AgencyCenterAreaBean>(
            _mActivity, R.layout.fragment_agency_item_center
        ) {
            override fun convert(
                holder: XQuickViewHolder?,
                item: AgencyCenterAreaBean?,
                position: Int
            ) {
                holder?.setText(R.id.titleTv, item?.title)
                    ?.setText(R.id.numTv, "${item?.num}")
                    ?.setOnClickListener(R.id.mCheck) {
                        if ((item?.num ?: 0) > 0) {
                            start(AgencyCountListFragment.newInstance(item))
                        } else {
                            normalShortToast(
                                _mActivity, "暂无${
                                    when (item?.type) {
                                        PROVINCE -> "省级代理"
                                        CITY -> "市级代理"
                                        DISTRICT -> "区县级代理"
                                        else -> ""
                                    }
                                }"
                            )
                        }

                    }
            }

        }
        mXGridLayout?.setAdapter(mAdapter, lifecycle)
        mXGridLayout?.maxItem = 3

        addList()

        val proxyIndex = _mActivity.getString(R.string.proxyIndex)

        HiDataBus.with<AgencyProxyIndexBean>(proxyIndex)
            .observerSticky(this, true, {

                ls.clear()
                val province = AgencyCenterAreaBean(
                    "已申抢省级代理",
                    it.shengProxyNums,
                    PROVINCE
                )

                val city = AgencyCenterAreaBean(
                    "已申抢市级代理",
                    it.shiProxyNums,
                    CITY
                )

                val district = AgencyCenterAreaBean(
                    "已申抢区县级代理",
                    it.quProxyNums,
                    DISTRICT
                )

                ls.add(province)
                ls.add(city)
                ls.add(district)

                addList()
            })
    }

    private fun addList() {
        mAdapter?.clear()
        mAdapter?.addAll(ls)
    }


}